package com.ycy.pass;

import com.ycy.BaseTest;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Test;

import java.util.*;

public class PassDemo extends BaseTest {

  @Test
  public void create() {
    Deployment deploy = repositoryService.createDeployment().addClasspathResource("pass/abc.bpmn20.xml").deploy();
    System.out.println(deploy.getId());
    //设置流程变量
    HashMap<String, Object> map = new HashMap<>();
    //定义的人员列表4人
    String[] v = {"shareniu1", "shareniu2", "shareniu3", "shareniu4"};
    map.put("per", "60元/位置");
    map.put("money", "10000元");

    List<String> zhixingrens = new ArrayList<>();
    zhixingrens.add("ycy");
    zhixingrens.add("xdm");
    map.put("ASSIGNEE_zhixingren1_LIST", zhixingrens);

    ProcessInstance pi = runtimeService.startProcessInstanceByKey("abc", map);
    System.out.println("==> 流程实例启动 实例id :" + pi.getProcessInstanceId());
  }

  @Test
  public void passB(){
    HashMap<String, Object> variables = new HashMap<>();
    List<String> zhixingrens2 = new ArrayList<>();
    zhixingrens2.add("ycy1");
    zhixingrens2.add("xdm1");
    variables.put("ASSIGNEE_zhixingren2_LIST", zhixingrens2);

    //或签 其他没审批的节点 assign 保留
    taskService.complete("5022",variables);
  }


  //审批 通过
  @Test
  public void pass() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      System.out.println((i + 1) + ") " + tasks.get(i).getId() + " : " + tasks.get(i).getName());
      System.out.println(tasks.get(i));

      //如果参数设置错误会怎么样？
      Map<String, Object> variables = new HashMap<String, Object>();
      variables.put("approved111111", true);
      taskService.complete(tasks.get(i).getId(), variables);
    }
  }

  //审批 通过
  @Test
  public void pass1() {
    TaskService taskService = processEngine.getTaskService();
    //查询
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      System.out.println((i + 1) + ") " + tasks.get(i).getId() + " : " + tasks.get(i).getName());
      System.out.println(tasks.get(i));

      //如果参数设置错误会怎么样？
      Map<String, Object> variables = new HashMap<String, Object>();
      variables.put("approved", true);
      taskService.complete(tasks.get(i).getId(), variables);
    }
  }

  //通过 task
  @Test
  public void passapproveTask(){

    TaskService taskService = processEngine.getTaskService();
    //查询
    List<Task> tasks = taskService.createTaskQuery().taskDefinitionKey("approveTask").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      System.out.println((i + 1) + ") " + tasks.get(i).getId() + " : " + tasks.get(i).getName());
      System.out.println(tasks.get(i));

      //如果参数设置错误会怎么样？
      Map<String, Object> variables = new HashMap<String, Object>();
      variables.put("approved", true);
      taskService.complete(tasks.get(i).getId(), variables);
    }
  }

  //审批 通过
  @Test
  public void pass2() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskDefinitionKey("holidayApprovedTask").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      System.out.println((i + 1) + ") " + tasks.get(i).getId() + " : " + tasks.get(i).getName());
      System.out.println(tasks.get(i));

      //如果参数设置错误会怎么样？
      Map<String, Object> variables = new HashMap<String, Object>();
      variables.put("approved", true);
      taskService.complete(tasks.get(i).getId(), variables);
    }
  }

  //审批 通过
  @Test
  public void passAll() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      System.out.println((i + 1) + ") " + tasks.get(i).getId() + " : " + tasks.get(i).getName());
      System.out.println(tasks.get(i));

      //如果参数设置错误会怎么样？
      Map<String, Object> variables = new HashMap<String, Object>();
      variables.put("approved", true);
      taskService.complete(tasks.get(i).getId(), variables);
    }
  }

}
